require(['./config'],()=>{
    require(['req','template','header','jquery','footer','zoom'],(req,template,header)=>{
       class Details{
           constructor(){
            this.count=1
             this.init()
             this.initNum()

           }
           //初始化input文本框显示数量
          initNum(){
              $('.num_mid').val(this.count)
          } 
           init(){
              const id=location.search.split('=')[1]
            req.getDetailsData(id)
         
            .then(res=>{
                if(res.code===200){
                   this.render(res.data)
                }
            })
           }
           render(data){
            //    console.log(data)
               const {detail:{descContentList,id,image,price,title,photo}}= data
               $('#dbmb').html(template('detailsTemplate',{id,image,price,title,photo}))

               $('.xq').html(
                   template('detailsXqTemplate',{list:descContentList}))

     
                   this.initZoom()
                   this.bindEvent()
                   this.addToCart({id,image,price,title,photo})
                   this.addToGm({id,image,price,title,photo})

           }
           //初始化放大镜
           initZoom(){
               $('.zoom-image').elevateZoom({
                   gallery:'smallimg'
               })
           }
           //绑定事件点击改变数量
           bindEvent(){
               
               $('.num_left').click(()=>{
                   if(this.count<=1){
                       return
                   }
                   this.count--
                   this.initNum()
                   return false
               })
               $('.num_right').click(()=>{
                   this.count++
                   this.initNum()
                   return false
               })
           }
           //点击加入购物车
           addToCart(res){
               $('.addBtn').click(()=>{
                   let prodList=localStorage.getItem('prodList')
                   let flag=false
                   if(prodList){
                     prodList=JSON.parse(prodList)
                     for(let i=0;i<prodList.length;i++){
                         if(prodList[i].id===res.id){
                             prodList[i].count+=this.count
                             flag=true
                             break
                         }
                     }
                     if(!flag){
                         prodList.push({...res,count:this.count,checked:true})
                     }
                     localStorage.setItem('prodList',JSON.stringify(prodList))
                    
                   }else{
                       const arr=[{...res,count:this.count,checked:true}]
                       localStorage.setItem('prodList',JSON.stringify(arr))
                   }


                   //让loading显示消失
                   $('.loading').fadeIn(1000,()=>{
                       $('.loading').fadeOut(1000)
                   })
               
                   //改变头部数量
                   header.changeCount()
               })
           }

//点击加入购买成功
           addToGm(res){
               $('.addBtn1').click(()=>{
                   let prodList=localStorage.getItem('prodList')
                   let flag=false
                   if(prodList){
                     prodList=JSON.parse(prodList)
                     for(let i=0;i<prodList.length;i++){
                         if(prodList[i].id===res.id){
                             prodList[i].count+=this.count
                             flag=true
                             break
                         }
                     }
                     if(!flag){
                         prodList.push({...res,count:this.count,checked:true})
                     }
                     localStorage.setItem('prodList',JSON.stringify(prodList))
                    
                   }else{
                       const arr=[{...res,count:this.count,checked:true}]
                       localStorage.setItem('prodList',JSON.stringify(arr))
                   }
                    //让gw显示消失
                    $('.gm').fadeIn(1000,()=>{
                        $('.gm').fadeOut(1000)
                    })
                   
                   //改变头部数量
                //    header.changeCount()
               })
           }

           

       }
       return new Details()
    })
})